package models

import (
	"gorm.io/gorm"
	"time"
	"user-srv/basic/inits"
)

// 用户勋章表
type UserBadges struct {
	Id         uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	UserId     uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:用户id;not null;" json:"user_id"`                           // 用户id
	RulesId    uint64         `gorm:"column:rules_id;type:bigint UNSIGNED;comment:勋章规则id;not null;" json:"rules_id"`                     // 勋章规则id
	ObtainedAt time.Time      `gorm:"column:obtained_at;type:datetime(3);comment:获得时间;default:CURRENT_TIMESTAMP(3);" json:"obtained_at"` // 获得时间
	Status     uint8          `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-有效 0-失效;not null;default:0;" json:"status"`      // 状态 1-有效 0-失效
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// 用户勋章表
func (ub *UserBadges) TableName() string {
	return "user_badges"
}

// 勋章添加
func (ub *UserBadges) UserBadgesAdd() error {
	return inits.DB.Create(&ub).Error
}

// 勋章展示
func (ub *UserBadges) UserBadgesFind() (UserBadgesFind []*UserBadges, err error) {
	err = inits.DB.Find(&UserBadgesFind).Error
	return
}

func (ub *UserBadges) GetUserBadgesUserId() error {
	return inits.DB.Where("user_id = ?", ub.UserId).Limit(1).Find(&ub).Error
}

func (ub *UserBadges) UserBadgesUpdate() error {
	return inits.DB.Model(&ub).Where("rules_id = ?", ub.RulesId).Update("status", ub.Status).Error
}
